package cn.edu.hnu.gpg.service;

import cn.edu.hnu.gpg.dao.BulletinMapper;
import cn.edu.hnu.gpg.dto.BulletinDisplay;
import cn.edu.hnu.gpg.entity.Administrator;
import cn.edu.hnu.gpg.entity.Bulletin;
import cn.edu.hnu.gpg.entity.BulletinExample;
import cn.edu.hnu.gpg.entity.User;
import cn.edu.hnu.gpg.entity.enums.UserType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BulletinServiceImpl implements BulletinService{

    private BulletinMapper bulletinMapper;

    @Autowired
    private AdminService adminService;

    @Autowired
    public BulletinServiceImpl(BulletinMapper bulletinMapper){
        this.bulletinMapper = bulletinMapper;
    }

    @Override
    public int createNewBulletin(String title, String content, User user) {
        if (user.getUserType()!= UserType.A)
            return 0;

        int admin_id = adminService.findAdminByUser(user.getUserId()).getAdminId();

        Bulletin bulletin = new Bulletin();
        bulletin.setTitle(title);
        bulletin.setContent(content);
        bulletin.setPublisher(admin_id);

        int affectedLine = bulletinMapper.insertSelective(bulletin);
        if (affectedLine < 1)
            throw new RuntimeException("Failed in inserting bulletin");

        return bulletin.getBulletinId();
    }

    @Override
    public int createNewBulletin(String title, String content, int user_id) {
        if (user_id < 1)
            return 0;

        Administrator administrator = adminService.findAdminByUser(user_id);
        if (administrator == null)
            return 0;

        Bulletin bulletin = new Bulletin();
        bulletin.setTitle(title);
        bulletin.setContent(content);
        bulletin.setPublisher(administrator.getAdminId());

        int affectedLine = bulletinMapper.insertSelective(bulletin);
        if (affectedLine < 1)
            throw new RuntimeException("Failed in inserting bulletin");

        return bulletin.getBulletinId();
    }

    @Override
    public List<Bulletin> listBulletin() {
        BulletinExample example = new BulletinExample();
        example.setOrderByClause("`release_time` DESC, `bulletin_id` DESC");
        return bulletinMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public Bulletin queryBulletinById(Integer bulletinId) {
        return bulletinMapper.selectByPrimaryKey(bulletinId);
    }

    @Override
    public boolean deleteBulletin(Date date,int publisher) {

        BulletinExample bulletinExample = new BulletinExample();
        bulletinExample.or().andReleaseTimeEqualTo(date).andPublisherEqualTo(publisher);
        int num = bulletinMapper.deleteByExample(bulletinExample);
        return num > 0 ;
    }

    @Override
    public boolean deleteBulletin(int bulletin_id) {
        if ( bulletin_id < 1 )
            return false;

        return bulletinMapper.deleteByPrimaryKey(bulletin_id) > 0 ;
    }
}
